<template>
  <div class="promotion">
    <div class="coupon-header">
      <span class="receive-coupon">领取优惠券</span>
      <el-button type="primary" @click="showRedeemDialog = true">兑换优惠券</el-button>
    </div>
    <div class="coupon-list">
      <div class="coupon-row" v-for="(row, index) in couponRows" :key="index" style="display: flex; justify-content: space-around;">
        <CouponItem 
          v-for="coupon in row" 
          :key="coupon.id" 
          :coupon="coupon" 
          @received="handleReceived"
          @receive="handleReceive">
        </CouponItem>
      </div>
    </div>
    <el-dialog v-model="showRedeemDialog" title="兑换优惠券" class="redeem-dialog" width="400px" :close-on-click-modal="false">
      <el-input v-model="redeemCode" placeholder="请输入8位英文加数字的兑换码" class="redeem-input"></el-input>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showRedeemDialog = false" class="cancel-btn">取消</el-button>
          <el-button type="primary" @click="handleRedeem" class="confirm-btn">兑换</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import CouponItem from '@/components/CouponItem.vue'
import promotionApi from '@/api/promotion'
import { CouponVO } from '@/type/coupon'

const coupons = ref<CouponVO[]>([])

const couponRows = computed(() => {
  const rows: CouponVO[][] = []
  for (let i = 0; i < coupons.value.length; i += 2) {
    rows.push(coupons.value.slice(i, i + 2))
  }
  return rows
})

// 获取优惠券列表
const fetchCoupons = () => {
  promotionApi.getCoupons().then((response: any) => {
    console.log(response.data)
    coupons.value = response.data.data || []
  }).catch((error: any) => {
    console.log(error)
    ElMessage.error('获取优惠券列表失败: ' + (error.message || '未知错误'))
  })
}

// 处理领取优惠券
const handleReceive = (id: string) => {
  promotionApi.receiveCoupon(id).then((response: any) => {
    // 更新本地状态
    const coupon = coupons.value.find(c => c.id === id)
    if (coupon) {
      coupon.received = true
    }
  }).catch((error: any) => {
    ElMessage.error('领取失败: ' + (error.message || '未知错误'))
  })
}

// 处理已领取事件（保持向后兼容）
const handleReceived = (id: string) => {
  const coupon = coupons.value.find(c => c.id === id)
  if (coupon) {
    coupon.received = true
    ElMessage.success('领取成功')
  }
}

const showRedeemDialog = ref(false)
const redeemCode = ref('')

// 处理兑换优惠券
const handleRedeem = () => {
  if (!redeemCode.value) {
    ElMessage.error('请输入兑换码')
    return
  }
  
  promotionApi.redeemCoupon(redeemCode.value).then((response: any) => {
    ElMessage.success('兑换成功')
    showRedeemDialog.value = false
    redeemCode.value = ''
    // 重新获取优惠券列表以更新状态
    fetchCoupons()
  }).catch((error: any) => {
    ElMessage.error('兑换失败: ' + (error.message || '未知错误'))
  })
}

onMounted(() => {
  fetchCoupons()
})
</script>

<style scoped>
.promotion {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.coupon-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  width: 100%;
}

.receive-coupon {
  font-size: 18px;
  font-weight: bold;
}

.coupon-list {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  width: 100%;
  justify-content: space-around;
}

.coupon-row {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
  width: 100%;
  justify-content: space-around;
}

.redeem-dialog {
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.redeem-input {
  margin: 16px 0;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.cancel-btn {
  border-radius: 4px;
}

.confirm-btn {
  border-radius: 4px;
  background-color: #409eff;
}
</style>